import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;

import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.SwingConstants;
import java.awt.Color;


public class SearchPanel extends JPanel {
	
	private JTextField txtSearchTerm;
	private JButton btnSearchName;
	JTabbedPane tabbedPane;
	private JLabel lblSearchDatabase;
	JButton btnEditUser;
	private JLabel lblSearchTerm;
	private JButton btnUpdate;
	private JButton btnSearchUsername;
	private JList patientlist;
	private int lastSearchType;
	private JList userlist;
	private JList adminlist;
	private JList doctorlist;
	private JList nurselist;
	private ScrollPane UserScroll;
	private ScrollPane AdminScroll;
	private ScrollPane DoctorScroll;
	private ScrollPane NurseScroll;
	private ScrollPane PatientScroll;
	public User editingUser = null;
	JLabel lblError;
	
	private class buttonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getSource().equals(btnSearchName) || 
					arg0.getSource().equals(btnSearchUsername)){
				if(arg0.getSource().equals(btnSearchName)) 
					lastSearchType = UserDatabase.NAME_SEARCH;
				else lastSearchType = UserDatabase.USERNAME_SEARCH;
				
				//Add to generic user list
				ArrayList<User> results = UserDatabase.searchUser(
						txtSearchTerm.getText(), lastSearchType);
				String[] names = new String[results.size()];
				for (int ind = 0; ind < results.size(); ind++) {
					names[ind] = results.get(ind).name;
				}
				userlist = new JList(names);
				userlist.setVisibleRowCount(-1);
				userlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				UserScroll.removeAll();
				UserScroll.add(userlist);
				
				//Add to patient list
				ArrayList<Patient> resultP = UserDatabase.searchPatient(
						txtSearchTerm.getText(), lastSearchType);
				names = new String[resultP.size()];
				for (int ind = 0; ind < resultP.size(); ind++) {
					names[ind] = resultP.get(ind).name;
				}
				patientlist = new JList(names);
				patientlist.setVisibleRowCount(-1);
				patientlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				PatientScroll.removeAll();
				PatientScroll.add(patientlist);
				
				//Add to doctor list
				ArrayList<Doctor> resultD = UserDatabase.searchDoctor(
						txtSearchTerm.getText(), lastSearchType);
				names = new String[resultD.size()];
				for (int ind = 0; ind < resultD.size(); ind++) {
					names[ind] = resultD.get(ind).name;
				}
				doctorlist = new JList(names);
				doctorlist.setVisibleRowCount(-1);
				doctorlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				DoctorScroll.removeAll();
				DoctorScroll.add(doctorlist);
				
				//Add to nurse list
				ArrayList<Nurse> resultN = UserDatabase.searchNurse(
						txtSearchTerm.getText(), lastSearchType);
				names = new String[resultN.size()];
				for (int ind = 0; ind < resultN.size(); ind++) {
					names[ind] = resultN.get(ind).name;
				}
				nurselist = new JList(names);
				nurselist.setVisibleRowCount(-1);
				nurselist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				NurseScroll.removeAll();
				NurseScroll.add(nurselist);
				
				//Add to admin list
				ArrayList<SystemAdmin> resultA = UserDatabase.searchAdmin(
						txtSearchTerm.getText(), lastSearchType);
				names = new String[resultA.size()];
				for (int ind = 0; ind < resultA.size(); ind++) {
					names[ind] = resultA.get(ind).name;
				}
				adminlist = new JList(names);
				adminlist.setVisibleRowCount(-1);
				adminlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				AdminScroll.removeAll();
				AdminScroll.add(adminlist);
				
			}
			else if(arg0.getSource().equals(btnUpdate)){
				if(lastSearchType == UserDatabase.NAME_SEARCH){
					ActionEvent e = new ActionEvent(btnSearchName, 
							ActionEvent.ACTION_PERFORMED, null);
					actionPerformed(e);
				}
				else if(lastSearchType == UserDatabase.USERNAME_SEARCH){
					ActionEvent e = new ActionEvent(btnSearchUsername, 
							ActionEvent.ACTION_PERFORMED, null);
					actionPerformed(e);
				}
			}
			else if(arg0.getSource().equals(btnEditUser)){
				ScrollPane check = (ScrollPane) tabbedPane.getSelectedComponent();
				if(check.equals(UserScroll) && userlist.getSelectedIndex() != -1){
					String searchTerm = userlist.getSelectedValue().toString();
					editingUser = UserDatabase.searchUser(searchTerm, UserDatabase.NAME_SEARCH).get(0);
				}
				else if(check.equals(PatientScroll) && patientlist.getSelectedIndex() != -1){
					String searchTerm = patientlist.getSelectedValue().toString();
					editingUser = UserDatabase.searchPatient(searchTerm, UserDatabase.NAME_SEARCH).get(0);
				}
				else if(check.equals(NurseScroll) && nurselist.getSelectedIndex() != -1){
					String searchTerm = nurselist.getSelectedValue().toString();
					editingUser = UserDatabase.searchNurse(searchTerm, UserDatabase.NAME_SEARCH).get(0);
				}
				else if(check.equals(DoctorScroll) && doctorlist.getSelectedIndex() != -1){
					String searchTerm = doctorlist.getSelectedValue().toString();
					editingUser = UserDatabase.searchDoctor(searchTerm, UserDatabase.NAME_SEARCH).get(0);
				}
				else if(check.equals(AdminScroll) && adminlist.getSelectedIndex() != -1){
					String searchTerm = adminlist.getSelectedValue().toString();
					editingUser = UserDatabase.searchAdmin(searchTerm, UserDatabase.NAME_SEARCH).get(0);
				}
				
				if(editingUser != null){
					lblSearchDatabase.getParent().firePropertyChange("editingUser", 0, 1);
				}
			}
			
		}
		
	}

	public SearchPanel() {
		setLayout(null);
		setPreferredSize(new Dimension(450, 550));
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(152, 112, 300, 360);
		add(tabbedPane);
		
		UserScroll = new ScrollPane();
		tabbedPane.addTab("All Users", null, UserScroll, null);
		
		userlist = new JList();
		UserScroll.add(userlist);
		
		AdminScroll = new ScrollPane();
		tabbedPane.addTab("Admins", null, AdminScroll, null);
		
		adminlist = new JList();
		AdminScroll.add(adminlist);
		
		DoctorScroll = new ScrollPane();
		tabbedPane.addTab("Doctors", null, DoctorScroll, null);
		
		doctorlist = new JList();
		DoctorScroll.add(doctorlist);
		
		NurseScroll = new ScrollPane();
		tabbedPane.addTab("Nurses", null, NurseScroll, null);
		
		nurselist = new JList();
		NurseScroll.add(nurselist);
		
		PatientScroll = new ScrollPane();
		tabbedPane.addTab("Patients", null, PatientScroll, null);
		
		patientlist = new JList();
		PatientScroll.add(patientlist);
		
		btnEditUser = new JButton("Edit User");
		btnEditUser.setBounds(12, 169, 114, 49);
		add(btnEditUser);
		btnEditUser.addActionListener(new buttonListener());
		
		btnUpdate = new JButton("Update List");
		btnUpdate.setBounds(12, 259, 114, 49);
		add(btnUpdate);
		btnUpdate.addActionListener(new buttonListener());
		
		btnSearchUsername = new JButton("Search by Username");
		btnSearchUsername.setBounds(278, 514, 174, 22);
		add(btnSearchUsername);
		btnSearchUsername.addActionListener(new buttonListener());
		
		btnSearchName = new JButton("Search by Name");
		btnSearchName.setBounds(278, 547, 174, 22);
		add(btnSearchName);
		btnSearchName.addActionListener(new buttonListener());
		
		txtSearchTerm = new JTextField();
		txtSearchTerm.setBounds(152, 514, 116, 22);
		add(txtSearchTerm);
		txtSearchTerm.setColumns(10);
		
		lblSearchDatabase = new JLabel("Search Database");
		lblSearchDatabase.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 28));
		lblSearchDatabase.setBounds(152, 31, 272, 34);
		add(lblSearchDatabase);
		
		lblSearchTerm = new JLabel("Search Term:");
		lblSearchTerm.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblSearchTerm.setHorizontalAlignment(SwingConstants.CENTER);
		lblSearchTerm.setBounds(12, 518, 114, 14);
		add(lblSearchTerm);
		
		lblError = new JLabel("");
		lblError.setHorizontalAlignment(SwingConstants.CENTER);
		lblError.setForeground(Color.RED);
		lblError.setFont(new Font("Tahoma", Font.ITALIC, 9));
		lblError.setBounds(12, 229, 114, 14);
		add(lblError);
	}
	public JButton getBtnSearchName() {
		return btnSearchName;
	}
	public JTabbedPane getTabbedPane() {
		return tabbedPane;
	}
	public JLabel getLblSearchDatabase() {
		return lblSearchDatabase;
	}
	public JButton getBtnEditUser() {
		return btnEditUser;
	}
	public JTextField getTxtSearchTerm() {
		return txtSearchTerm;
	}
	public ScrollPane getPatientScroll() {
		return PatientScroll;
	}
	public JLabel getLblSearchTerm() {
		return lblSearchTerm;
	}
	public JButton getBtnUpdate() {
		return btnUpdate;
	}
	public JButton getBtnSearchUsername() {
		return btnSearchUsername;
	}
	public JList getPatientlist() {
		return patientlist;
	}
	public JList getAdminlist() {
		return adminlist;
	}
	public JList getDoctorlist() {
		return doctorlist;
	}
	public JList getNurselist() {
		return nurselist;
	}
	public JList getUserlist() {
		return userlist;
	}
	public ScrollPane getUserScroll() {
		return UserScroll;
	}
	public ScrollPane getAdminScroll() {
		return AdminScroll;
	}
	public ScrollPane getDoctorScroll() {
		return DoctorScroll;
	}
	public ScrollPane getNurseScroll() {
		return NurseScroll;
	}
	
	public String toString(){
		return "userSearch";
	}
}
